﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;
using System.Collections;
using System.IO;
using System.Diagnostics;

namespace QuanLyNhaSach_1173005
{
    public partial class frmDanhSachKhachHang : Form
    {
        public frmDanhSachKhachHang()
        {
            InitializeComponent();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            KhachHangDTO kh = new KhachHangDTO();
            kh.MaKhachHang = "KH" + DateTime.Now.Ticks;
            kh.HoTenKhachHang = txt_TenKhachHang.Text;
            kh.DiaChi = txt_DiaChi.Text;
            kh.DienThoai = txt_DienThoai.Text;
            kh.Email = txt_Email.Text;
            KhachHangBUS.ThemKH(kh);
            MessageBox.Show("Thêm thể loại mới thành công", "Quản lý nhà sách");
            LoadKH();
        }
        private void LoadKH()
        {
            dtgv_HienThiSach.Rows.Clear();
            ArrayList arr = KhachHangBUS.LayDanhSach();
            for (int i = 0; i < arr.Count; i++)
            {
                KhachHangDTO dv = (KhachHangDTO)arr[i];
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell cellSTT = new DataGridViewTextBoxCell();
                cellSTT.Value = i + 1;
                DataGridViewTextBoxCell cellMaKhachHang = new DataGridViewTextBoxCell();
                cellMaKhachHang.Value = dv.MaKhachHang;
                DataGridViewTextBoxCell cellTenKhachHang = new DataGridViewTextBoxCell();
                cellTenKhachHang.Value = dv.HoTenKhachHang;
                DataGridViewTextBoxCell cellDiaChi = new DataGridViewTextBoxCell();
                cellDiaChi.Value = dv.DiaChi;
                DataGridViewTextBoxCell cellDienThoai = new DataGridViewTextBoxCell();
                cellDienThoai.Value = dv.DienThoai;
                DataGridViewTextBoxCell cellEmail = new DataGridViewTextBoxCell();
                cellEmail.Value = dv.Email;
                row.Cells.Add(cellSTT);
                row.Cells.Add(cellMaKhachHang);
                row.Cells.Add(cellTenKhachHang);
                row.Cells.Add(cellDiaChi);
                row.Cells.Add(cellDienThoai);
                row.Cells.Add(cellEmail);
                dtgv_HienThiSach.Rows.Add(row);
            }
        }

        private void frmDanhSachKhachHang_Load(object sender, EventArgs e)
        {
            LoadKH();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String makh = dtgv_HienThiSach.SelectedRows[0].Cells["MaKhachHang"].Value.ToString();
                KhachHangDTO kh = KhachHangBUS.LayThongTinKhachHang(makh);
                kh.MaKhachHang = txt_MaKhachHang.Text;
                kh.HoTenKhachHang = txt_TenKhachHang.Text;
                kh.DiaChi = txt_DiaChi.Text;
                kh.DienThoai = txt_DienThoai.Text;
                kh.Email = txt_Email.Text;
                KhachHangBUS.CapNhatKH(kh);
                MessageBox.Show("Cập nhật thông tin khách hàng thành công", "Quản lý nhà sách");
                LoadKH();
            }
            else
            {
                MessageBox.Show("Chưa chọn khách hàng để cập nhật", "Quản lý nhà sách");
            }
        }

        private void dtgv_HienThiSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String makh = dtgv_HienThiSach.SelectedRows[0].Cells["MaKhachHang"].Value.ToString();
                KhachHangDTO kh = KhachHangBUS.LayThongTinKhachHang(makh);
                txt_MaKhachHang.Text = kh.MaKhachHang;
                txt_TenKhachHang.Text = kh.HoTenKhachHang;
                txt_DiaChi.Text = kh.DiaChi;
                txt_DienThoai.Text = kh.DienThoai;
                txt_Email.Text = kh.Email;
            }
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String makh = dtgv_HienThiSach.SelectedRows[0].Cells["MaKhachHang"].Value.ToString();
                KhachHangDTO kh = KhachHangBUS.LayThongTinKhachHang(makh);
                kh.MaKhachHang = txt_MaKhachHang.Text;
                KhachHangBUS.XoaKH(kh);
                MessageBox.Show("Xóa khách hàng thành công", "Quản lý nhà sách");
                LoadKH();
            }
            else
            {
                MessageBox.Show("Chưa chọn khách hàng để xóa", "Quản lý nhà sách");
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_PhieuMoi_Click(object sender, EventArgs e)
        {
            KhachHangDTO pn = new KhachHangDTO();
            txt_MaKhachHang.Text = "";
            txt_TenKhachHang.Text = "";
            txt_Email.Text = "";
            txt_DienThoai.Text = "";
            txt_DiaChi.Text = "";
            //btn_CapNgat.Enabled = false;
            //btn_XoaPhieu.Enabled = false;
            //dtgv_HienThiSach.Rows.Clear();
            //KTLoi();
        }

        private void btn_export_Click(object sender, EventArgs e)
        {
            string oldPath = Directory.GetCurrentDirectory();
            SaveFileDialog sf = new SaveFileDialog();
            sf.ShowDialog();
            string strFileName = sf.FileName;
            Directory.SetCurrentDirectory(oldPath);
            ExcelWrite myExcel = new ExcelWrite();
            List<String> list = myExcel.DGVToExcel(dtgv_HienThiSach);
            String filePath = Directory.GetCurrentDirectory() + strFileName;
            myExcel.WriteDateToExcel(strFileName, list, "A1", "F1", "A2");
            MessageBox.Show("Export dữ liệu thành công...", "Thông báo");
        }
    }
}
